Skip to content

9. Embedded programming

This week’s group assignments write programs in multiple development environments, boards, and languages.

Things to do

Use different boards to operate the circuit consisting of LEDs.

C

used FabISP.
wrote the following code with C.

#include <avr/io.h>
#include <util/delay.h>

int main(){
    DDRA = 0b10000000; // portA setting
    DDRB = 0b00001011; // protB setting

    while(1){
        if(bit_is_clear(PINB,PINB2)){
            PORTA = 0b10000000;
        }else{
            PORTA = 0b00000000;
        }
    }    
}

Arduino

designed the following circuit. arduino

wrote the following code with C++.

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

Raspberry Pi

raspberrypi

wrote the following code with python.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(2,GPIO.OUT)
for x in xrange(5):
    GPIO.output(2,True)
    time.sleep(x)
    GPIO.output(2,False)
    time.sleep(x)
GPIO.cleanup()

micro:bit

microbit

Impression

Oops,The image was not taken. It might be because the code to write was too easy, but I did not feel the difference in the degree of difficulty in writing the program.